package com.zabra.zl.ui.recharge.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.RechargeOffLineBean;
import com.zabra.zl.data.RechargeOnLineBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.data.WechatBean;
import com.zabra.zl.ui.recharge.contract.RechargeContract;
import com.zabra.zl.ui.recharge.model.RechargeModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RechargePresenter extends BasePresenter<RechargeContract.View> implements RechargeContract.Presenter {
    private RechargeContract.Model model;
    public RechargePresenter() {
        model=new RechargeModel();
    }

    @Override
    public void getOnLineData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getOnLineData(body).enqueue(new Callback<RechargeOnLineBean>() {
            @Override
            public void onResponse(Call<RechargeOnLineBean> call, Response<RechargeOnLineBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<RechargeOnLineBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getOffLineData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getOffLineData(body).enqueue(new Callback<RechargeOffLineBean>() {
            @Override
            public void onResponse(Call<RechargeOffLineBean> call, Response<RechargeOffLineBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<RechargeOffLineBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getOffLineSubmitData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getOffLineSubmitData(body).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getWeChatData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getWeChatData(body).enqueue(new Callback<WechatBean>() {
            @Override
            public void onResponse(Call<WechatBean> call, Response<WechatBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<WechatBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getSubmitWeChatData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getSubmitWeChatData(body).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getAlipayData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getAlipayData(body).enqueue(new Callback<WechatBean>() {
            @Override
            public void onResponse(Call<WechatBean> call, Response<WechatBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<WechatBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getSubmitAlipayData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getSubmitAlipayData(body).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
